//index.js
//获取应用实例
const app = getApp()
const datas = require("../../data/driverMockData.js");
var handler = require("../../utils/function.js"),
tcity = require("../../data/citys.js"),
api = require("../../api.js"),
WxParse = require('../../wxParse/wxParse.js');
Page({
    data: {
      order_url: [
        "/pages/driverOrder/orderedStatus/orderedStatus",
        "/pages/driverOrder/orderGoing/orderGoing",
        "/pages/driverOrder/bill/bill",
        "/pages/driverOrder/orderDetail/orderDetail",
        "/pages/driverOrder/orderedStatus/orderedStatus",
      ],
      test: app.test,
        Img: handler.driverImg,
        addressName: null,
        avatar: "../../../images/driver/icon-02.png",
        activityList: [],
        orderCount: 0,
        currentTime: null,
        income: 0.00,
        reminder: "今日周一，无限号通知，多云25-30，事宜短袖。每日6点会更新数据信息。新的一天，不要忘记微笑哦。",
        name: "",
        orderCount: 0,
        income: 0.00,
        evaluate: [],
        myTask: [],
        // handler: [],
        serverPhone: "",
        left: 700,
        is_false: false,
        remind: '',
        way: 0,
        authorization: 1,//关注服务号
        work_status:"",
       todayincome:'0.00',
       mOrder:[],
       orderno:'',
       is_service:0,
    },
    toDetail (url){
      var url = url.currentTarget.dataset.url;
      wx.navigateTo({
        url
      })
    },
  get_wxGZH() {
    let that = this;
    if (wx.getStorageSync("access_token") && wx.getStorageSync("is_as") != 1) {
      app.request({
        url: api.user.binding,
        method: 'post',
        data: {},
        success: function (rs) {
          console.log(rs.data)
          if (rs.data.authorization != 1) {
            that.setData({ authorization: rs.data.authorization })
          }
        }
      })
    }
  },
  navTo(e){
    app.navTo(e.currentTarget.dataset.url)
  },
  navtoHome(){
    wx.navigateTo({
      url:'/pages/map/driverServer/driverServer'
    })
  },
  onLoad: function (options) {
    //关注服务号
    this.get_wxGZH();
    setInterval(this.timeTask, 1000);
    // 获取优惠券
    if (options.uid) {
      app.shareCouponsMessage(options.uid)
    }
    },
    // 时间定时器
    timeTask: function() {
        const date = new Date();
        this.setData({
            currentTime: {
                h: date.getHours(),
                m: date.getMinutes()
            }
        });
    },
    onShow: function() {
        let that=this;
        handler.getLatLntHandler((res) => {
          // console.log(res,'222222222');
          that.setData({
            addressName: res.address, //开始位置
          })
        });
        this.setDataHandler();
        this.getDataHnadler();
      //  判断是否上班
      app.request({
        url: api.driver.getset,
        method: 'post',
        data: {},
        success: function (rs) {
          that.setData({
            work_status: rs.data.work_status
          })
        }
      })

      // m-订单
      app.request({
        url: api.order_branch.getTodayOrder,
          method: 'post',
          data: {},
          success: function(rs) {
              if (rs.code == 1) {
                  let _data = rs.data.data
                  _data = _data.filter(item=>item.is_pay==="0")
                  console.log(_data)
                  if(_data.length){
                    let orderno = _data[0]['order_no']
                    let is_service = _data[0]['is_service']
                    that.setData({
                      mOrder: _data,
                      orderno:orderno,
                      is_service:is_service
                    });
                  }
                  
              }
          }
      })

    },
    xingChengFn(){
      // 跳转行程详细页面
      let orderno = this.data.orderno
      let is_service = this.data.is_service
      let order_url = this.data.order_url
      wx.navigateTo({
        url:order_url[is_service] + "?order_no=" + orderno
      })
    },
    // 今日接单
    gotoTodayInforamtion:function(){
      wx.navigateTo({
        url: '/pages/driver/todayOrder/index',
      })
    },
    getDataHnadler: function() {
        var that = this;
      var _this = this;
        app.request({
            url: api.driver.index,
            method: 'post',
            data: {},
            success: function(rs) {
                if (rs.data.is_complete == 0) {
                  wx.redirectTo({
                    url: '../../../../driver/personalInformation/personalInformation',
                  })
                }
              var announcement = rs.data.announcement;
              for (var i = 0; i < rs.data.announcement.length;i++){
                WxParse.wxParse('article', 'html', announcement[i].content, _this, 5)
              }
                that.setData({
                    orderCount: rs.data.Todayorder,
                    income: rs.data.income,
                    todayincome: rs.data.todayincome,
                    activityList: rs.data.announcement,
                    is_false: rs.data.reward,
                    way: rs.data.way,
                    like:rs.data.like,
                })
                wx.setStorageSync("customer", rs.data.customer);
              wx.setStorageSync("like", rs.data.like);
            }
        })
    },
    // setDataHandler 渲染数据函数
    setDataHandler: function() {
        const avatar = wx.getStorageSync("userMessage") !== "" ? wx.getStorageSync("userMessage").avatar : "../../../images/icon/icon-02.png";
        const name = wx.getStorageSync("userMessage") !== "" ? wx.getStorageSync("userMessage").username : "";
        this.setData({
            avatar,
            evaluate: wx.getStorageSync("evaluate").length,
            name
        });
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
        // console.log(e.currentTarget.dataset.skip);
        handler.intoPageHandler(e.currentTarget.dataset.skip)
    },
    // callPhoneHandler 打电话功能
    callPhoneHandler: function() {
        handler.callPhoneHandler(wx.getStorageSync("serverphone"))
    },
    // intoPersonCenterHandler 个人中心
    intoPersonCenterHandler: function() {
        handler.intoPageHandler('../driver/personalCenter/personalCenter')
    },
    // getOrderHandler 开始抢单
    getOrderHandler: function(e) {
      let that=this;
        if (e.currentTarget.dataset.way == 1) {
            wx.showToast({
                title: '听单方式为指派接单',
                icon:'none',
                duration: 1000,
            })
        } else if (that.data.work_status==1){
          wx.showToast({
            title: '目前是下班状态，不能接单',
            icon: 'none',
            duration: 1000,
          })
        } else {
            const orderStatus = wx.getStorageSync("setup");
            // console.log('抢单', orderStatus)
            
            wx.navigateTo({
              url: '../driverOrder/getOrder/getOrder?like=' + that.data.like,
            })
          // if (orderStatus.listenWay === "抢单模式" && orderStatus.orderHobby == "预约"){
          //   wx.navigateTo({
          //     url: '../driverOrder/appointmentOrder/appointmentOrder',
          //   })
          // }
        }
    },
})